// import Cookies from 'js-cookie'
import {
  save,
  load
} from '../utils'
// import $srv from '@/server';
import $store from '../store'
import {
  guid,
  guid2,
  shortGuid2,
  getUUID
} from './index.js';

export {
  guid,
  guid2,
  shortGuid2,
  getUUID
}
from './index.js';

export function getToken() {
  let tk = load("token")
  return tk
}

export function setToken(token) {
  return save("token", token)
}

export function removeToken() {
  return save("token")
}

export function configRequestData(data) {
  data = data || {}
  const {
    app,
    device
  } = $configs //see index.html
  const uuid = getUUID();
  const token = getToken()
  if (data instanceof FormData) {
    let jio = {
      app,
      uuid,
      device
    }
    if (token) {
      jio.token = token
    }
    let s = JSON.stringify(jio)
    data.append("jio", s)
  } else {
    data = {...data}
    data.app = app;
    data.uuid = uuid;
    data.device = device;
    if (token) {
      data.token = token
    }
  }
  // console.log(data)
  return data
}

export function getRoles() {
  let store = $store;
  let roles = store.getters.roles
  return roles
}

export async function getUserinfo() {
  let store = $store;
  let token = getToken();
  let data = await $srv.request("user_get", {
    token
  });
  if (data.success) {
    let user = data.rss.user;
    await store.dispatch("user/setUser", user);
    return user;
  }
  return data;
}

export function login(redir) {
  redir = redir || window.location.href
  let arr = redir.split("?redir=");
  if (arr.length > 1) {
    redir = arr[arr.length - 1]
  } else {
    redir = encodeURIComponent(redir)
  }
  let url = `/zz-aac/#login?redir=${redir}`
  window.open(url, "_self")
}

export async function logout(redir) {
  let store = $store;
  await store.dispatch("user/clearAccout");
  login(redir);
}

export function pvLog(from, to) {
  return $srv.request("audit_pv_log_add", {
    from: from.path || '',
    to: to.path || '',
  }).then(data => {
    // console.log("pvLog", data);
  })
}

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
// function hasPermission(roles, route) {
//   if (route.meta && route.meta.roles) {
//     return roles.some(role => route.meta.roles.includes(role))
//   } else {
//     return true
//   }
// }
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role.name || role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route
    }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

window.addEventListener('storage', function(e) {
  console.log('storage', e); //须为同一浏览器打开的两个同源页面
})
